package net.airtheva.uniqueTask2;

import java.util.ArrayList;

import net.airtheva.uniqueTask2.fragment.FollowersFragment;
import net.airtheva.uniqueTask2.fragment.IUserFragment;
import net.airtheva.uniqueTask2.fragment.ProfileFragment;
import net.airtheva.uniqueTask2.fragment.TweetsFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Toast;

import com.viewpagerindicator.TabPageIndicator;

public class UserActivity extends FragmentActivity {

	class LocalPagerAdapter extends FragmentPagerAdapter {
		
		public LocalPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragments.get(position).first;
        }

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			return (Fragment) mFragments.get(position).second;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mFragments.size();
		}
		
		public void Update() {
			
			for(int i = 0; i < mFragments.size(); i++) {
				mFragments.get(i).second.Update();
			}
			
		}
		
	}
	
	public static final int REQUEST_CODE_SETUP = 0;
	public static final int REQUEST_CODE_WRITE_TWEET = 1;
	public static final int REQUEST_CODE_VIEW_USER = 2;
	
	String mSession = null;
	int mUserId = -1;
	
	ArrayList<Pair<String, IUserFragment>> mFragments = null;
	
	LocalPagerAdapter mAdapter = null;
	ViewPager mViewPager = null;
	
	JSONObject mProfile = null;
	JSONArray mTweets = null;
	JSONArray mFollowers = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(savedInstanceState == null) {
			
			mSession = getIntent().getStringExtra("session");
			mUserId = getIntent().getIntExtra("userId", API.USER_ID_UNKNOWN);
			
		}
		else {
			
			mSession = savedInstanceState.getString("session");
			mUserId = savedInstanceState.getInt("userId", API.USER_ID_UNKNOWN);
			
		}
		
		setContentView(R.layout.activity_user);
		
		mFragments = new ArrayList<Pair<String,IUserFragment>>();
		mFragments.add(new Pair<String, IUserFragment>("微博列表", new TweetsFragment()));
		mFragments.add(new Pair<String, IUserFragment>("个人资料", new ProfileFragment()));
		mFragments.add(new Pair<String, IUserFragment>("粉丝列表", new FollowersFragment()));
		
		mAdapter = new LocalPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.activity_user_pager);
        mViewPager.setAdapter(mAdapter);
        
        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.activity_user_indicator);
        indicator.setViewPager(mViewPager);
        indicator.setVisibility(View.GONE);
        
        LoadProfile();
        LoadTweets();
        LoadFollowers();
        
        ChangePage(1);
        
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
		if(resultCode == RESULT_OK) {
			
			switch(requestCode) {
			case REQUEST_CODE_SETUP:
				mAdapter.Update();
				break;
			case REQUEST_CODE_WRITE_TWEET:
				mAdapter.Update();
				break;
			}
			
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		outState.putString("session", mSession);
		outState.putInt("userId", mUserId);
		super.onSaveInstanceState(outState);
	}

	public void LoadProfile() {
		Response res = API.GetInstance(this).FetchProfile(mSession, mUserId);
		switch(res.GetStatusCode()) {
		case Response.STATUS_CODE_SUCCESS:
			try {
				mProfile = new JSONObject(res.GetExtra());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			Toast.makeText(UserActivity.this, res.GetExtra(), Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	public void LoadTweets() {
		Response res = API.GetInstance(this).FetchTweets(mSession, mUserId);
		switch(res.GetStatusCode()) {
		case Response.STATUS_CODE_SUCCESS:
			try {
				mTweets = new JSONArray(res.GetExtra());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			Toast.makeText(UserActivity.this, res.GetExtra(), Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	public void LoadFollowers() {
		Response res = API.GetInstance(this).FetchFollowers(mSession, mUserId);
		switch(res.GetStatusCode()) {
		case Response.STATUS_CODE_SUCCESS:
			try {
				mFollowers = new JSONArray(res.GetExtra());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			Toast.makeText(UserActivity.this, res.GetExtra(), Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	public JSONObject GetProfile() {
		
		return mProfile;
		
	}

	public JSONArray GetTweets() {
		
		return mTweets;
		
	}
	
	public JSONArray GetFollowers() {
		
		return mFollowers;
		
	}
	
	public void ChangePage(int position) {
		mViewPager.setCurrentItem(position);
	}
	
	public void WriteTweet() {
		
		Intent intent = new Intent(this, WriteTweetActivity.class);
		intent.putExtra("session", mSession);
		startActivityForResult(intent, REQUEST_CODE_WRITE_TWEET);
		
	}
	
	public void Setup() {
		
		Intent intent = new Intent(this, SetupActivity.class);
		intent.putExtra("return", true);
		intent.putExtra("session", mSession);
		startActivityForResult(intent, REQUEST_CODE_SETUP);
		
	}
	
	public void ViewUser(int userId) {
		
		Intent intent = new Intent(this, UserActivity.class);
		intent.putExtra("session", mSession);
		intent.putExtra("userId", userId);
		startActivityForResult(intent, REQUEST_CODE_VIEW_USER);
		
	}

}
